package com.cpj.bsyyzx.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cpj.bsyyzx.entity.CaseHistory;
import com.cpj.bsyyzx.entity.HealthFile;
import com.cpj.bsyyzx.service.ICaseHistoryService;
import com.cpj.bsyyzx.service.IHealthFileService;
import com.cpj.bsyyzx.util.CommonResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 五个臭皮匠
 * @since 2021-04-27
 */
@RestController
@RequestMapping("/caseHistory")
public class CaseHistoryController {

    @Resource
    ICaseHistoryService caseHistoryService;

    //    @RequestMapping("/list")
//    @ResponseBody
//    CommonResult pagelist(Page page){
//        QueryWrapper<HealthFile> queryWrapper = new QueryWrapper<>();
////        log.debug("分页查询");
//        IPage<HealthFile> rpage=healthFileService.selectPageVo(page,queryWrapper);
//        return CommonResult.success(rpage);
//    }
    @GetMapping("/list")
    @ResponseBody
    CommonResult page(Page page, CaseHistory caseHistory){

        QueryWrapper queryWrapper = new QueryWrapper();
        if(caseHistory!= null && StringUtils.isNotEmpty(caseHistory.getUserName())){
            queryWrapper.like("user_name",caseHistory.getUserName());
        }
        return CommonResult.success(caseHistoryService.page(page,queryWrapper));
    }



    @RequestMapping("/update")
    @ResponseBody
    CommonResult update(CaseHistory caseHistory) {
        return CommonResult.success(caseHistoryService.updateById(caseHistory));
    }

    @RequestMapping("/add")
    @ResponseBody
    CommonResult add(CaseHistory caseHistory) {
        if(caseHistoryService.add(caseHistory))
            return CommonResult.success("ok");
        else
            return CommonResult.failed("姓名已经存在");
    }

    @RequestMapping("toedit")
    @ResponseBody
    CommonResult toedit(Integer historyId) {
        return CommonResult.success(caseHistoryService.getById(historyId));
    }
    @RequestMapping("delete/{historyId}")
    @ResponseBody
    CommonResult delete(@PathVariable Integer historyId) {
//        log.debug("bbb根据主键删除:" + id);
        boolean success = caseHistoryService.removeById(historyId);
        return CommonResult.success(success);
    }
    @RequestMapping("/batchdel")
    @ResponseBody
    CommonResult batchdel(Integer[] id) {
        return CommonResult.success(caseHistoryService.removeByIds(Arrays.asList(id)));
    }

}
